﻿/*
=============================================================================
Author:魏宝辉通过CodeSmithe生成
CreatedTime:2012-10-16
Description:Login_Log三层结构数据访问层
=============================================================================
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using FTchina.Models;//引入模型层
namespace FTchina.DAL
{
	public static partial class Login_LogService
	{
		#region 增加一条数据
		public static int AddLogin_Log(Login_Log login_Log)
		{
			string sql =
			"INSERT INTO Login_Log(uname,loginip,logindate,version,utype,result) "+
			"VALUES (@uname,@loginip,@logindate,@version,@utype,@result)";
			
			 
				SqlParameter[] para = new SqlParameter[]
				{
						
					new SqlParameter("@uname",login_Log.Uname),
						
					new SqlParameter("@loginip",login_Log.Loginip),
						
					new SqlParameter("@logindate",login_Log.Logindate),
						
					new SqlParameter("@version",login_Log.Version),
						
					new SqlParameter("@utype",login_Log.Utype),
						
					new SqlParameter("@result",login_Log.Result)
				};
				int newId = DBHelper.ExecuteCommand(sql, para);
				return newId;
			 
		}
		#endregion
		#region 删除数据以主键，标识列，唯一列构建的删除
			
		public static int DeleteLogin_LogById(long id)	
		{
			string sql="DELETE "+"Login_Log"+" Where id=@id";
			 SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id", id)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}		
		
		#endregion
		#region 修改数据
		//考虑标识列与主键不一致情形
		//以标识列修改实体
		public static int ModifyLogin_LogByid(Login_Log login_Log)
		{
		string sql ="UPDATE "+"Login_Log"+" set "+
					"uname=@uname, "+
					"loginip=@loginip, "+
					"logindate=@logindate, "+
					"version=@version, "+
					"utype=@utype, "+
					"result=@result "+ 
				" Where id=@id";
			 
				SqlParameter[] para = new SqlParameter[]
				{
					new SqlParameter("@id",login_Log.Id),
					new SqlParameter("@uname",login_Log.Uname),
					new SqlParameter("@loginip",login_Log.Loginip),
					new SqlParameter("@logindate",login_Log.Logindate),
					new SqlParameter("@version",login_Log.Version),
					new SqlParameter("@utype",login_Log.Utype),
					new SqlParameter("@result",login_Log.Result)
				};
				return DBHelper.ExecuteCommand(sql, para);
			 
		}					
		#endregion
		#region 查询数据
		public static IList<Login_Log> GetAllLogin_Logs()
		{
			string sqlAll = "SELECT * FROM Login_Log";
			return GetLogin_LogsBySql(sqlAll);
		}
		//以标识列获得对象
		public static Login_Log GetLogin_LogByid(object id)
		{
			string sql = "SELECT * FROM "+"Login_Log"+" WHERE id = @id";
			
            using (SqlDataReader reader = DBHelper.GetReader(sql, new SqlParameter("@id", id)))
			{
				 
				if (reader.Read())
				{
					Login_Log login_Log = new Login_Log();
					login_Log.Id=Convert.ToInt32(reader["id"]);
					login_Log.Uname=Convert.ToString(reader["uname"]);
					login_Log.Loginip=Convert.ToString(reader["loginip"]);
					login_Log.Logindate=Convert.ToDateTime(reader["logindate"]);
					login_Log.Version=Convert.ToString(reader["version"]);
					login_Log.Utype=Convert.ToString(reader["utype"]);
					login_Log.Result=Convert.ToString(reader["result"]);
					reader.Close();
					
					return login_Log;
					}
				else
				{
					reader.Close();
					return null;
				}
			}
			 
		}
					
		//私有方法供构建好的SQL语句调用
		private static IList<Login_Log> GetLogin_LogsBySql(string safeSql)
		{
			List<Login_Log> list = new List<Login_Log>();
			 
            using (DataTable table = DBHelper.GetDataTable(safeSql))
			{
				foreach (DataRow row in table.Rows)
				{
					Login_Log login_Log = new Login_Log();
					login_Log.Id=Convert.ToInt32(row["id"]);
					login_Log.Uname=Convert.ToString(row["uname"]);
					login_Log.Loginip=Convert.ToString(row["loginip"]);
					login_Log.Logindate=Convert.ToDateTime(row["logindate"]);
					login_Log.Version=Convert.ToString(row["version"]);
					login_Log.Utype=Convert.ToString(row["utype"]);
					login_Log.Result=Convert.ToString(row["result"]);
					list.Add(login_Log);
				}
				return list;
			}
             
		}
		private static IList<Login_Log> GetLogin_LogsBySql( string sql, params SqlParameter[] values )
		{
			List<Login_Log> list = new List<Login_Log>();
			 
            using (DataTable table = DBHelper.GetDataTable(sql, values))
			{
				foreach (DataRow row in table.Rows)
				{
					Login_Log login_Log = new Login_Log();
					login_Log.Id=Convert.ToInt32(row["id"]);
					login_Log.Uname=Convert.ToString(row["uname"]);
					login_Log.Loginip=Convert.ToString(row["loginip"]);
					login_Log.Logindate=Convert.ToDateTime(row["logindate"]);
					login_Log.Version=Convert.ToString(row["version"]);
					login_Log.Utype=Convert.ToString(row["utype"]);
					login_Log.Result=Convert.ToString(row["result"]);
					list.Add(login_Log);
				}
				return list;
			}
            
			 
		}
		#endregion
		#region 分页的方法page为当前页码，NUMCount为每页数据
		public static IList<Login_Log> ShowLogin_LogForPage(int curragePage,int NUMCount)
		{
			int number = 0;
			if (curragePage > 1) {
				number = NUMCount * (curragePage - 1);
			}
			string strsql= "select top "
						   +NUMCount+" * from Login_Log where  id not in ( select top "
						   +number+" id from Login_Log order by id desc ) order by id desc";
			return GetLogin_LogsBySql(strsql);
		}
		#endregion
	}
}

